package com.soyea.config;

import com.soyea.handler.LogHandler;
import com.soyea.handler.MsgHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by skh on 2018/1/18
 */
@Configuration
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {

	@Autowired
	private WechatMpProperties properties;

	@Autowired
	protected LogHandler logHandler; //日志处理器

	@Autowired
	protected MsgHandler msgHandler; //消息处理器

	@Bean
	public WxMpConfigStorage configStorage() {
		WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
		configStorage.setAppId(properties.getAppId());
		configStorage.setSecret(properties.getSecret());
		configStorage.setToken(properties.getToken());
		configStorage.setAesKey(properties.getAesKey());
		return configStorage;
	}

	@Bean
	public WxMpService wxMpService(WxMpConfigStorage configStorage) {
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(configStorage);
		return wxMpService;
	}

	//消息路由器
	@Bean
	public WxMpMessageRouter router(WxMpService wxMpService) {
		final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

		// 记录所有事件的日志 （异步执行）
		newRouter.rule().handler(this.logHandler).next();

		// 接收客服会话管理事件
//		newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//				.event(WxMpEventConstants.CustomerService.KF_CREATE_SESSION)
//				.handler(this.kfSessionHandler).end();
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(WxMpEventConstants.CustomerService.KF_CLOSE_SESSION)
//				.handler(this.kfSessionHandler)
//				.end();
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(WxMpEventConstants.CustomerService.KF_SWITCH_SESSION)
//				.handler(this.kfSessionHandler).end();

		// 门店审核事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(WxMpEventConstants.POI_CHECK_NOTIFY)
//				.handler(this.storeCheckNotifyHandler).end();

		// 自定义菜单事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(MenuButtonType.CLICK).handler(this.getMenuHandler()).end();

		// 点击菜单连接事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(MenuButtonType.VIEW).handler(this.nullHandler).end();

		// 关注事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(EventType.SUBSCRIBE).handler(this.getSubscribeHandler())
//				.end();

		// 取消关注事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(EventType.UNSUBSCRIBE)
//				.handler(this.getUnsubscribeHandler()).end();

		// 上报地理位置事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(EventType.LOCATION).handler(this.getLocationHandler())
//				.end();

		// 接收地理位置消息
//		newRouter.rule().async(false).msgType(XmlMsgType.LOCATION)
//				.handler(this.getLocationHandler()).end();

		// 扫码事件
//		newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//				.event(EventType.SCAN).handler(this.getScanHandler()).end();

		// 默认 兜底路由规则，一般放到最后
		newRouter.rule().async(false).handler(msgHandler).end();

		return newRouter;
	}


}
